package cn.csust.net2.manager.client.ux;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.csust.net2.manager.client.util.ClassTools;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.service.BaseServiceAsync;
import cn.csust.net2.manager.shared.util.Factory;
import cn.csust.net2.manager.shared.util.Utils;
import cn.csust.net2.manager.shared.xml.PropertyAssist;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.google.gwt.i18n.client.DateTimeFormat;

public class ColumnFactory {
	
	public static <T extends PO> ColumnModel createColumn(PO po,final BaseServiceAsync service) {
		
			GridCellRenderer<BeanModel>	 gridNumber = new GridCellRenderer<BeanModel>() {  
		      public String render(BeanModel model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<BeanModel> store, Grid<BeanModel> grid) {  
		    	 // double val = (Double) model.get(property);  
//		    	  if(model.getBean() instanceof KK )
//		    		  
		          String style = "red";  
		          return "<span style='color:" + style + "'>" + model.get(property) + "</span>";  
		      }  
		    }; 
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		RowNumberer rowNumber = new RowNumberer();
		columns.add(rowNumber);
		CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
		columns.add(sm.getColumn());

		//渲染表格中文字的颜色，如果是当前学期之前的学期的数据就显示为红色，不允许用户修改
		GridCellRenderer<BeanModel> colorRender = new GridCellRenderer<BeanModel>() {
			public String render(BeanModel model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<BeanModel> store, Grid<BeanModel> grid) {
				Object value = model.get(property);
				if(value!=null) {
					String valueString = value.toString();
					if(value instanceof Date) {
						valueString = Utils.dateTimeFormat((Date)value);
						System.out.println(valueString);
					} 
					Date createDate = model.get("createDate");
					int old = -1;
					if(createDate!=null) {
						Date now = new Date();
						if(!now.after(createDate)) {
							old = rowIndex;
						}
					}
					if(rowIndex==old) {
						return "<span style='color:red'>"+valueString+"</span>";
					}
					return valueString;
				} else {
					return "";
				}
				
				
			}
		};
		
		//是否审核通过的图片渲染
		GridCellRenderer<BeanModel> imageRender = new GridCellRenderer<BeanModel>() {
			public String render(BeanModel model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<BeanModel> store, Grid<BeanModel> grid) {
				String val = model.get(property);  
				if(val!=null && val.equals("yes")) {
					return "<img title='审核通过' src='images/yes.png' />";
				} else if(val!=null && val.equals("no")) {
					return "<img title='审核未通过' src='images/no.gif' />";
				} else {
					return "<img title='未审核' src='images/not.gif' />";
				}
				
			}
		};
		List<PropertyAssist> list = po.getAssist().getPropertyAssists();
		for (PropertyAssist propertyAssist : list) {
			if (propertyAssist.getDisplayName() == null
					|| propertyAssist.getDisplayName().equals("")) {
				continue;
			}
			ColumnConfig columnConfig = new ColumnConfig();
			
			
			columnConfig.setRenderer(gridNumber);
			
			
			columnConfig.setId(propertyAssist.getName());
			columnConfig.setHeader(propertyAssist.getDisplayName());
			
			boolean blankable = propertyAssist.isBlankable();
			boolean readOnly = propertyAssist.isReadOnly();
			
			//设值每一列的单元格编辑器
			if (propertyAssist.getEditorType() == EditorType.DateField) {
				DateField dateField = Factory.createDateFieldWithFormated();
				dateField.setAllowBlank(blankable);
				dateField.setReadOnly(readOnly);
				columnConfig.setEditor(new CellEditor(dateField));
				columnConfig.setDateTimeFormat(DateTimeFormat
						.getFormat("yyyy年MM月dd日"));
				
			} else if (propertyAssist.getEditorType() == EditorType.NumberField_Long) {
				NumberField numberField = new NumberField();
				numberField.setAllowBlank(blankable);
				numberField.setReadOnly(readOnly);
				numberField.setPropertyEditorType(Long.class);
				columnConfig.setEditor(new CellEditor(numberField));
			} else if (propertyAssist.getEditorType() == EditorType.NumberField_Double) {
				NumberField numberField = new NumberField();
				numberField.setAllowBlank(blankable);
				numberField.setReadOnly(readOnly);
				numberField.setPropertyEditorType(Double.class);
				columnConfig.setEditor(new CellEditor(numberField));
			}else if (propertyAssist.getEditorType() == EditorType.NumberField_Integer) {
				NumberField numberField = new NumberField();
				numberField.setAllowBlank(blankable);
				numberField.setReadOnly(readOnly);
				numberField.setPropertyEditorType(Integer.class);
				columnConfig.setEditor(new CellEditor(numberField));
			}else if (propertyAssist.getEditorType() == EditorType.SimpleBox) {
				final SimpleComboBox<String> combo = Utils
						.createSimpleComboBox(propertyAssist.getSimpleBox());
				combo.setForceSelection(true);
				combo.setReadOnly(readOnly);
				combo.setTriggerAction(TriggerAction.ALL);
				CellEditor editor = new CellEditor(combo) {
					@Override
					public Object preProcessValue(Object value) {
						if (value == null) {
							return value;
						}
						return combo.findModel(value.toString());
					}

					@Override
					public Object postProcessValue(Object value) {
						if (value == null) {
							return value;
						}
						return ((ModelData) value).get("value");
					}
				};
				columnConfig.setEditor(editor);
			} else if (propertyAssist.getEditorType() == EditorType.BeanCombo) {

				// final BeanCombo<T> combo = new BeanCombo<T>(comboCla);
				
				PO guanlian = ClassTools.newInstance(propertyAssist
						.getProperties().get("class"));
				final BeanCombo combo = new BeanCombo(guanlian, service);
				combo.setAllowBlank(blankable);
				combo.setReadOnly(readOnly);
				CellEditor editor = new CellEditor(combo) {
					@Override
					public Object preProcessValue(Object value) {
						if (value == null) {
							return value;
						}
						return combo.findBeanModel(null, value.toString());
					}

					@Override
					public Object postProcessValue(Object value) {
						if (value == null) {
							return value;
						}
						return combo.getValue().getBean();
					}
				};
				columnConfig.setEditor(editor);
			} else { // 默认是TextField编辑器
				TextField<String> textField = new TextField<String>();
				textField.setAllowBlank(blankable);
				if(propertyAssist.getName().equals("state")) {
					textField.setEnabled(false);
				}
				columnConfig.setEditor(new CellEditor(textField));
			}
			
			//渲染
			columnConfig.setRenderer(colorRender);
			if(propertyAssist.getName().equals("state")) {
				columnConfig.setRenderer(imageRender);
			}
			columns.add(columnConfig);
		}
		return new ColumnModel(columns);
	}
}
